package com.power.web.action.member;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionContext;
import com.power.Constants;
import com.power.model.aboutus.TsAboutus;
import com.power.model.member.TmMember;
import com.power.model.user.TsSysuser;
import com.power.services.aboutus.TsAboutusService;
import com.power.services.district.TsDistrictService;
import com.power.services.hospital.HospitalService;
import com.power.services.member.TmMemberService;
import com.power.services.user.SysuserService;
import com.power.util.BASE64Util;
import com.power.util.CharacterUtil;
import com.power.util.DateJsonValueProcessor;
import com.power.util.RequestUtil;
import com.power.util.mail.MailInfo;
import com.power.util.mail.MailUtil;
import com.power.web.action.base.AbstractBaseAction;

/**
 * 
 * 会员action
 * 
 */
public class TmMemberAction extends AbstractBaseAction {

	private TmMember tmMember;
	private TsSysuser tsSysuser;
	private String jsonString;
	private String tempId;
	@Autowired
	private TmMemberService tmMemberService;
	@Autowired
	private SysuserService userService;
	@Autowired
	@Qualifier("hospitalService")
	private HospitalService hospitalService;
	@Autowired
	@Qualifier("tsDistrictService")
	private TsDistrictService tsDistrictService;
	private int reLogin;
	private List proList;
	private List cityList;
	private String mailChangeFlag;
	@Autowired
    @Qualifier("tsAboutusService")
    private TsAboutusService tsAboutusService;
    private TsAboutus tsAboutus;
	

	/**
	 * 增加管理员
	 * 
	 * @return
	 */
	public String addMember() {
		try {
			String json = "{failure:true,msg:'会员新增加失败！'}";
			if (tempId == null || "".equals(tempId)) {
				List<TsSysuser> list = userService
						.findByLogonnameWithDate("",tsSysuser.getUsername());
				if (list != null && list.size() > 0) {
					this.response.reset();
					this.response.setCharacterEncoding("UTF-8");
					this.response.getWriter().print(
							"{success:false,msg:'登录名已经存在!'}");
					return null;
				}
			}
			TsSysuser loginUser = null;
			Object obj = this.session.get(Constants.USERINFO_KEY);
			if (obj != null) {
				loginUser = (TsSysuser) obj;
			}
			if (tempId == null || "".equals(tempId)) {
				tsSysuser.setInputDate(new Date());
				tsSysuser.setPassword(BASE64Util.getBASE64(tsSysuser
						.getPassword()));
				tmMember.setTmInputDate(new Date());
			} else {
				// 修改处理
				TmMember oldTm = tmMemberService.findTmMemberById(tempId);
				if (null != oldTm) {
					tmMember.setLoginId(Integer.parseInt(tempId));
					tmMember.setTmInputDate(oldTm.getTmInputDate());
				}else{
					tmMember.setTmInputDate(new Date());
				}
				TsSysuser oldUser = userService.read(oldTm.getLoginId());
				tsSysuser.setInputDate(oldUser.getInputDate());
				tsSysuser.setId(oldTm.getLoginId());
			}
			tsSysuser.setCategory("1");
			tmMemberService.createTmMember(tmMember, tsSysuser, loginUser);
			json = "{success:true, msg:'会员添加成功！'}";
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(json);
		} catch (Exception e) {
			log.error("会员新增加失败！", e);
		}
		return null;
	}

	/**
	 * 修改会员
	 * 
	 * @return
	 */
	public String updateMemberInfo() {
		try {
			String json = "{failure:true,msg:'会员信息修改失败！'}";
			if ((null!=tempId || !"".equals(tempId))&&"1".equals(mailChangeFlag)) {
				List<TmMember> list = tmMemberService.findEmailExisist(tmMember.getEmail());
				if (list != null && list.size() > 0) {
					this.response.reset();
					this.response.setCharacterEncoding("UTF-8");
					this.response.getWriter().print(
							"{success:false,msg:'电子邮箱已经存在!'}");
					return null;
				}
			}
			TmMember oldTm = tmMemberService.findTmMemberById(tempId);
			oldTm.setEmail(tmMember.getEmail());
			oldTm.setRealName(tmMember.getRealName());
			tmMemberService.update(oldTm);
			json = "{success:true, msg:'会员信息修改成功！'}";
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(json);
		} catch (Exception e) {
			log.error("会员新增加失败！", e);
		}
		return null;
	}
	
	/**
	 * 查询
	 * 
	 * @return
	 */
	public String searchMember() {
		try {
			int start = this.start;
			int limit = this.limit;
			if (limit == 0)
				limit = 20;
			Map<String, String> paraMap = new HashMap<String, String>();
			String category = RequestUtil.getParam(request, "category", "1");
			paraMap.put("start", start + "");
			paraMap.put("limit", limit + "");
			paraMap.put("category", category);
			Map<String, Object> dataMap = tmMemberService.findTmMember(paraMap);
			StringBuffer jsonStr = new StringBuffer();
			String callback = this.callback;
			List<Object[]> data = (List<Object[]>) dataMap.get("data");
			int count = Integer.parseInt(dataMap.get("count").toString());
			List list = new ArrayList();
			for (Object[] obj : data) {
				TmMember tmMember = (TmMember) obj[0];
				TsSysuser tsSysuser = (TsSysuser) obj[1];
				tsSysuser.setMemberid(tmMember.getTemId());
				tsSysuser.setRealName(tmMember.getRealName());
				tsSysuser.setDistrictId(tmMember.getDistrictId());
				tsSysuser.setDistrictName(tmMember.getDistrictName());
				tsSysuser.setEmail(tmMember.getEmail());
				tsSysuser.setIdcard(tmMember.getIdcard());
				tsSysuser.setPhone(tmMember.getPhone());
				tsSysuser.setProviceName(tmMember.getProviceName());
				tsSysuser.setDistrictName(tmMember.getDistrictName());
				tsSysuser.setSex(tmMember.getSex());
				tsSysuser.setInputMan(tmMember.getInputMan());
				tsSysuser.setTmInputDate(tmMember.getTmInputDate());
				list.add(tsSysuser);
			}
			jsonStr.append(callback).append("({\"total\":").append(count)
					.append(",\"results\":");
			JsonConfig jsonconfig = new JsonConfig();
			jsonconfig.registerJsonValueProcessor("inputDate",
					new DateJsonValueProcessor("yyyy-MM-dd kk:mm:ss"));
			jsonconfig.registerJsonValueProcessor(java.util.Date.class,
					new DateJsonValueProcessor("yyyy-MM-dd"));
			jsonconfig.registerJsonValueProcessor(java.sql.Date.class,
					new DateJsonValueProcessor("yyyy-MM-dd"));
			jsonconfig.registerJsonValueProcessor("tmInputDate",
					new DateJsonValueProcessor("yyyy-MM-dd kk:mm:ss"));
			JSONArray jsonObject = JSONArray.fromObject(list, jsonconfig);
			jsonStr.append(jsonObject.toString());
			jsonStr.append("})");
			this.jsonString = jsonStr.toString();

		} catch (Exception e) {
			log.error("会员查询失败！", e);
			this.jsonString = "[]";
		}
		this.response.setCharacterEncoding("UTF-8");
		try {
			this.response.getWriter().print(jsonString);
		} catch (IOException e) {
			log.error("会员查询io异常!", e);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * 删除会员
	 * 
	 * @return
	 */
	public String delMember() {
		try {
			String json = "{failure:true,msg:'会员删除失败！'}";
			String deleteIds = request.getParameter("deleteID");
			tmMemberService.deleteMember(deleteIds);
			json = "{success:true, msg:'会员删除成功！'}";
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(json);
		} catch (Exception e) {
			log.error("会员删除失败！", e);
		}
		return null;
	}

	/**
	 * 
	 * 加锁解锁
	 * 
	 * @return
	 */
	public String operLock() {
		String flag = null;

		try {
			String id = request.getParameter("id");
			flag = request.getParameter("flag");
			String oper = "解锁";
			if ("2".equals(flag)) {
				oper = "锁定";
			}
			String json = "{failure:true,msg:'会员" + oper + "失败！'}";
			tmMemberService.updateMemberLock(id, flag);
			json = "{success:true, msg:'会员" + oper + "成功！'}";
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(json);
		} catch (Exception e) {
			if ("2".equals(flag)) {
				log.error("会员锁定失败！", e);
			} else {
				log.error("会员解锁失败！", e);
			}
		}
		return null;
	}

	/**
	 * 
	 * 修改密码
	 * 
	 * @return
	 */
	public String modifyPass() {
		try {
			String json = "{failure:true,msg:'会员密码修改失败！'}";
			String id = request.getParameter("id");
			String password = request.getParameter("pass");
			tmMemberService.updatePassword(id, BASE64Util.getBASE64(password));
			json = "{success:true, msg:'会员密码修改成功！'}";
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(json);
		} catch (Exception e) {
			log.error("会员密码修改失败！", e);
		}
		return null;
	}

	/**
	 * 判断用户名是否已经存在
	 * 
	 * @return
	 */
	public String checkLoginName() {
		try {
			String json = "{result:false}";
			String loginName = request.getParameter("loginName");
			List<TsSysuser> list = userService
					.findByLogonnameWithDate("",loginName);
			if (list != null && list.size() > 0) {
				json = "{result:true}";
			}
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(json);
		} catch (Exception e) {
			log.error("验证用户名是否存在失败！", e);
		}
		return null;
	}

	/**
	 * 判断邮箱是否已经存在
	 * 
	 * @return
	 */
	public String checkEmail() {
		try {
			String json = "{result:false}";
			String email = request.getParameter("email");
			List<TmMember> list = tmMemberService.findEmailExisist(email);
			if (list != null && list.size() > 0) {
				json = "{result:true}";
			}
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(json);
		} catch (Exception e) {
			log.error("验证邮箱是否存在失败！", e);
		}
		return null;
	}

	/**
	 * 验证验证码
	 * 
	 * @return
	 */
	public String checkCode() {
		try {
			String json = "{result:false}";
			String code = request.getParameter("code");
			String scode = (String) request.getSession().getAttribute("rand");
			if (scode.equals(code)) {
				json = "{result:true}";
			}
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(json);
		} catch (Exception e) {
			log.error("验证验证码失败！", e);
		}
		return null;

	}

	/**
	 * 会员注册
	 * 
	 * @return
	 */
	public String registerMember() {
		try {
			String json = "{msg:false}";
			tsSysuser.setUsername(randLoginName());
			tsSysuser.setInputDate(new Date());
			tmMember.setTmInputDate(new Date());
			tmMemberService.createRegisterTmMember(tmMember, tsSysuser);
			json = "{msg:true}";
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(json);
		} catch (Exception e) {
			log.error("会员注册失败！", e);
		}
		return null;
	}

	/**
	 * 会员注册 form 表单提交
	 * 
	 * @return
	 */
	public String registerMember1() {
		try {
			// tsSysuser = new TsSysuser();
			tsSysuser.setUsername(tmMember.getEmail());
			tsSysuser
					.setPassword(BASE64Util.getBASE64(tsSysuser.getPassword()));
			tsSysuser.setInputDate(new Date());
			tmMember.setTmInputDate(new Date());
			tsSysuser.setLock("2");
			tmMember.setInputMan("用户自己注册");
			tsSysuser.setCategory("2");
			tmMember.setProviceName(hospitalService.getProName(tmMember
					.getDistrictId()));
			tmMember.setDistrictName(hospitalService.getDistrictName(tmMember
					.getDistrictId()));
			tsSysuser.setMemberid(tmMember.getTemId());
			tsSysuser.setDistrictId(tmMember.getDistrictId());
			tsSysuser.setDistrictName(tmMember.getDistrictName());
			tsSysuser.setEmail(tmMember.getEmail());
			tsSysuser.setIdcard(tmMember.getIdcard());
			tsSysuser.setPhone(tmMember.getPhone());
			tsSysuser.setProviceName(tmMember.getProviceName());
			tsSysuser.setDistrictName(tmMember.getDistrictName());
			tsSysuser.setSex(tmMember.getSex());
			tsSysuser.setProviceId(tmMember.getProviceId());
			boolean flag = registerSendMail(tsSysuser,tmMember);
			if(flag){
				Integer loginId = tmMemberService.createRegisterTmMember(tmMember,
					tsSysuser);
				tmMember.setLoginId(loginId);
			}
			// this.session.put(Constants.USERINFO_KEY, tsSysuser);
		} catch (Exception e) {
			log.error("会员注册失败！", e);
		}
		return "skipPage";
	}

	/**
	 * 会员信息 前台修改
	 * 
	 * @return
	 */
	public String updateMember() {
		try {
			TmMember tm = tmMemberService.read(tmMember.getTemId());
			tm.setPhone(tmMember.getPhone());
			tm.setDistrictId(tmMember.getDistrictId());
			tm.setIdcard(tmMember.getIdcard());
			tmMemberService.update(tm);
			tmMember.setProviceName(hospitalService.getProName(tmMember
					.getDistrictId()));
			tmMember.setDistrictName(hospitalService.getDistrictName(tmMember
					.getDistrictId()));
		} catch (Exception e) {
			log.error("会员注册失败！", e);
		}
		return "updateSuccess";
	}

	/**
	 * 检测邮箱是否存在
	 * 
	 * @return
	 */
	public String checkEmailIsExistMember() {
		String email = RequestUtil.getParam(request, "email", "");
		List<TmMember> list = null;
		try {
			list = tmMemberService.findEmailExisist(email);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if (null == list || list.size() == 0) {
			reLogin = 1;// 该用户名不存在
		} else {
			reLogin = 0;// 该用户名存在
		}
		try {
			response.getWriter().print(reLogin);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 检测手机号是否存在
	 * 
	 * @return
	 */
	public String checkPhoneIsExistMember() {
		String email = RequestUtil.getParam(request, "phone", "");
		List<TmMember> list = null;
		try {
			list = tmMemberService.findPhoneExisist(email);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if (null == list || list.size() == 0) {
			reLogin = 1;// 该用户名不存在
		} else {
			reLogin = 0;// 该用户名存在
		}
		try {
			response.getWriter().print(reLogin);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 检测身份证号是否存在
	 * 
	 * @return
	 */
	public String checkIdcardIsExistMember() {
		String email = RequestUtil.getParam(request, "idcard", "");
		List<TmMember> list = null;
		try {
			list = tmMemberService.findIdcardExisist(email);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if (null == list || list.size() == 0) {
			reLogin = 1;// 该用户名不存在
		} else {
			reLogin = 0;// 该用户名存在
		}
		try {
			response.getWriter().print(reLogin);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 随机产生登入名
	 * 
	 * @return
	 */
	private String randLoginName() {
		String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		char[] c = s.toCharArray();
		Random random = new Random();
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < 6; i++) {
			buffer.append(c[random.nextInt(c.length)]);
		}
		return buffer.toString();
	}

	/**
	 * 重置密码
	 * 
	 * @return
	 */
	public String resetPassword() {
		try {
			String json = "{result:false}";
			String email = request.getParameter("email");
			TsSysuser tsUser = tmMemberService.findEmailByUser(email);
			if(null==tsUser){
				json = "{result:0}";
				this.response.setCharacterEncoding("UTF-8");
				this.response.getWriter().print(json);
				return null;
			}
			if("2".equals(tsUser.getLock())){
				json = "{result:2}";
				this.response.setCharacterEncoding("UTF-8");
				this.response.getWriter().print(json);
				return null;
			}
			TmMember mem = tmMemberService.findTmMemberById(tsUser.getId().toString());
			if (tsUser != null) {
				String paramMail = BASE64Util.getBASE64(email);
				MailInfo mailInfo = new MailInfo();
				mailInfo.setSmtpServer("smtp.126.com");
//				mailInfo.setMailFrom("the_doctor_online@126.com");
				mailInfo.setUserName("rosham");
//				mailInfo.setPassword("test123");
				mailInfo.setMailFrom("rosham@126.com");
				mailInfo.setMailTo(email);
//				mailInfo.setUserName("医院在线系统");
				mailInfo.setPassword("zhenghao1011");
				mailInfo.setTitle("医生在线系统 – 密码找回！");
				mailInfo
						.setText("亲爱的用户，您好！感谢您一直以来对我们的支持。\r\n \r\n");
				mailInfo.setText(mailInfo.getText() + "                        我们收到了您的密码重置请求。请点击该链接重置您的密码："+ "  \r\n \r\n");
				mailInfo.setText(mailInfo.getText() + "                        http://www.9898sj.com/attendances/web/login/resetPassword.jsp?bWFpbA="+paramMail+ "  \r\n \r\n");
				mailInfo
				.setText(mailInfo.getText()
						+ "                           如果不能点击该链接地址，请复制并粘贴到浏览器的地址输入框\r\n\r\n");
				mailInfo.setText(mailInfo.getText() + "                        如果您要放弃以上修改，或未曾申请密码重置，请忽略并/或删除本邮件。"+ "  \r\n \r\n");
				mailInfo.setText(mailInfo.getText()
						+ "                                         感谢您使用医院在线系统\r\n");
				mailInfo
						.setText(mailInfo.getText()
								+ "--------------------------------------------------------------------------------------------------\r\n\r\n");
				mailInfo.setText(mailInfo.getText()
						+ "                        此信由系统发出，不接收回信，请勿直接回复。\r\n\r\n");
				mailInfo
						.setText(mailInfo.getText()
								+ "                        如有任何疑问，请与客服人员联系或者访问http://www.9898sj.com/attendances与我们取得联系。\r\n\r\n\r\n");
				mailInfo.setText(mailInfo.getText()
						+ "                                                                医生在线系统版权所有");
				if (MailUtil.sendMail(mailInfo)) {
					json = "{result:1}";
				}
				this.response.setCharacterEncoding("UTF-8");
				this.response.getWriter().print(json);
			}
		} catch (Exception e) {
			log.error("邮件发送失败！", e);
		}
		return null;
	}
	
	/**
	 * 账户激活
	 * @return
	 */
	public String wakeIDs(){
		try {
//			String email = RequestUtil.getParam(request, "email", "");
			String email = request.getQueryString();
			if("".equals(email)){
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().print("<script>alert('激活失败');</script>");
				return null;
			}else
				email = email.substring(7);
			TsSysuser tsUser = tmMemberService.findEmailByUser(BASE64Util.getFromBASE64(email));
			if(null==tsUser){
				this.response.setCharacterEncoding("UTF-8");
				response.getWriter().print("<script>alert('激活失败');</script>");
				return null;
			}
			tsUser.setLock("1");
			try {
				userService.update(tsUser);
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().print("<script>alert('激活成功');window.location.href='http://www.9898sj.com/attendances';</script>");
			} catch (RuntimeException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			log.error("账户激活失败！", e);
		}
		return null;
	}
	/**
	 * 重置密码
	 * 
	 * @return
	 */
	public boolean registerSendMail(TsSysuser tsUser,TmMember mem) {
		boolean flag = false;
		try {
			String email = mem.getEmail();
			if (tsUser != null) {
				String paramMail = BASE64Util.getBASE64(email);
				MailInfo mailInfo = new MailInfo();
				mailInfo.setSmtpServer("smtp.126.com");
//				mailInfo.setMailFrom("the_doctor_online@126.com");
				mailInfo.setUserName("rosham");
//				mailInfo.setPassword("test123");
				mailInfo.setMailFrom("rosham@126.com");
				mailInfo.setMailTo(email);
//				mailInfo.setUserName("医院在线系统");
				mailInfo.setPassword("zhenghao1011");
				mailInfo.setTitle("医生在线系统 – 账户激活！");
				mailInfo
						.setText("亲爱的用户，您好！感谢您使用本系统。\r\n \r\n");
				mailInfo.setText(mailInfo.getText() + "                        我们收到了您的账户激活请求。请点击该链接季候您的账号："+ "  \r\n \r\n");
				mailInfo.setText(mailInfo.getText() + "                        http://www.9898sj.com/attendances/member/wakeIDs.action?bWFpbA="+paramMail+ "  \r\n \r\n");
				mailInfo
				.setText(mailInfo.getText()
						+ "                           如果不能点击该链接地址，请复制并粘贴到浏览器的地址输入框\r\n\r\n");
				mailInfo.setText(mailInfo.getText()
						+ "                                         感谢您使用医院在线系统\r\n");
				mailInfo
						.setText(mailInfo.getText()
								+ "--------------------------------------------------------------------------------------------------\r\n\r\n");
				mailInfo.setText(mailInfo.getText()
						+ "                        此信由系统发出，不接收回信，请勿直接回复。\r\n\r\n");
				mailInfo
						.setText(mailInfo.getText()
								+ "                        如有任何疑问，请与客服人员联系或者访问http://www.9898sj.com/attendances与我们取得联系。\r\n\r\n\r\n");
				mailInfo.setText(mailInfo.getText()
						+ "                                                                医生在线系统版权所有");
				if (MailUtil.sendMail(mailInfo)) {
					flag = true;
				}
			}
		} catch (Exception e) {
			log.error("邮件发送失败！", e);
		}
		return flag;
	}
	
	/**
	 * 重置密码
	 * 
	 * @return
	 */
	public String updatePassword() {
		try {
			String json = "{result:false}";
			String email = RequestUtil.getParam(request, "email", "");
			if("".equals(email)){
				json = "{result:0}";
				this.response.setCharacterEncoding("UTF-8");
				this.response.getWriter().print(json);
				return null;
			}else
				email = email.substring(7);
			String pass = RequestUtil.getParam(request, "pass", "");
			if("".equals(pass)){
				json = "{result:0}";
				this.response.setCharacterEncoding("UTF-8");
				this.response.getWriter().print(json);
				return null;
			}
			TsSysuser tsUser = tmMemberService.findEmailByUser(BASE64Util.getFromBASE64(email));
			if(null==tsUser){
				json = "{result:0}";
				this.response.setCharacterEncoding("UTF-8");
				this.response.getWriter().print(json);
				return null;
			}
			tsUser.setPassword(BASE64Util.getBASE64(pass));
			try {
				userService.update(tsUser);
				json = "{result:1}";
				this.response.setCharacterEncoding("UTF-8");
				this.response.getWriter().print(json);
			} catch (RuntimeException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			log.error("密码重置失败！", e);
		}
		return null;
	}

	
	/**
	 * show会员信息
	 * 
	 * @return
	 */
	public String showMemInfo() {
		TsSysuser userInfo = (TsSysuser) this.session
				.get(Constants.USERINFO_KEY);
		tmMember = tmMemberService
				.findTmMemberById(userInfo.getId().toString());
		tmMember.setProviceName(hospitalService.getProName(tmMember
				.getDistrictId()));
		tmMember.setDistrictName(hospitalService.getDistrictName(tmMember
				.getDistrictId()));
		return "showMemInfo";
	}

	/**
	 * 修改会员信息
	 * 
	 * @return
	 */
	public String memInfomodify() {
		String id = request.getParameter("id");
		tmMember = tmMemberService.findTmMemberById(id);
		tmMember.setProviceId(hospitalService
				.getProId(tmMember.getDistrictId()));
		tmMember.setProviceName(hospitalService.getProName(tmMember
				.getDistrictId()));
		tmMember.setDistrictName(hospitalService.getDistrictName(tmMember
				.getDistrictId()));
		List proList = tsDistrictService.findAllPro();
		List cityList = tsDistrictService.findAllCityByProId1(tmMember
				.getDistrictId());
		ActionContext.getContext().put("proList", proList);
		ActionContext.getContext().put("cityList", cityList);
		ActionContext.getContext().put("proviceId", tmMember.getProviceId());
		ActionContext.getContext().put("cityId", tmMember.getDistrictId());
		return "memInfomodify";
	}

	public String loadCityByPid() {
		String id = request.getParameter("id");
		try {
			cityList = tsDistrictService.findAllCityByProId2(Integer
					.valueOf(id));
			StringBuffer sb = new StringBuffer();
			sb.append(net.sf.json.JSONArray.fromObject(cityList));
			this.response.setCharacterEncoding("UTF-8");
			response.getWriter().print(sb.toString());
		} catch (Exception e) {
			log.error("加载城市出错", e);
			try {
				response.getWriter().print("[]");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 会员信息查询
	 * 
	 * @return
	 */
	public String searchMemberList() {
		try {
			int start = this.start;
			int limit = this.limit;
			if (limit == 0)
				limit = 20;
			String loginName = CharacterUtil.encode(RequestUtil.getParam(
					request, "loginName", ""));
			String category = RequestUtil.getParam(request, "category", "1");
			Map<String, String> paraMap = new HashMap<String, String>();
			paraMap.put("start", start + "");
			paraMap.put("limit", limit + "");
			paraMap.put("loginName", loginName);
			paraMap.put("category", category);
			Map<String, Object> dataMap = tmMemberService.findTmMember(paraMap);
			StringBuffer jsonStr = new StringBuffer();
			String callback = this.callback;
			List<Object[]> data = (List<Object[]>) dataMap.get("data");
			int count = Integer.parseInt(dataMap.get("count").toString());
			List list = new ArrayList();
			for (Object[] obj : data) {
				TmMember tmMember = (TmMember) obj[0];
				TsSysuser tsSysuser = (TsSysuser) obj[1];
				tsSysuser.setMemberid(tmMember.getTemId());
				tsSysuser.setRealName(tmMember.getRealName());
				tsSysuser.setDistrictId(tmMember.getDistrictId());
				tsSysuser.setDistrictName(tmMember.getDistrictName());
				tsSysuser.setEmail(tmMember.getEmail());
				tsSysuser.setIdcard(tmMember.getIdcard());
				tsSysuser.setPhone(tmMember.getPhone());
				tsSysuser.setProviceName(tmMember.getProviceName());
				tsSysuser.setDistrictName(tmMember.getDistrictName());
				tsSysuser.setSex(tmMember.getSex());
				list.add(tsSysuser);
			}
			StringBuffer _JSONStr = null;
			_JSONStr = new StringBuffer();
			_JSONStr.append(callback).append("({\"total\":").append(count)
					.append(",\"results\":");
			JsonConfig jsonconfig = new JsonConfig();
			jsonconfig.registerJsonValueProcessor("inputDate",
					new DateJsonValueProcessor("yyyy-MM-dd kk:mm:ss"));
			jsonconfig.registerJsonValueProcessor(java.util.Date.class,
					new DateJsonValueProcessor("yyyy-MM-dd"));
			jsonconfig.registerJsonValueProcessor(java.sql.Date.class,
					new DateJsonValueProcessor("yyyy-MM-dd"));
			jsonconfig.registerJsonValueProcessor("tmInputDate",
					new DateJsonValueProcessor("yyyy-MM-dd kk:mm:ss"));
			JSONArray jsonObject = JSONArray.fromObject(list, jsonconfig);
			_JSONStr.append(jsonObject.toString());
			_JSONStr.append("})");
			jsonString = _JSONStr.toString();
		} catch (Exception e) {
			log.error("会员查询失败！", e);
			this.jsonString = "[]";
		}
		this.response.setCharacterEncoding("UTF-8");
		try {
			this.response.getWriter().print(jsonString);
		} catch (IOException e) {
			log.error("会员查询io异常!", e);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * web修改密码
	 * 
	 * @return
	 */
	public String modifyWebPass() {
		try {
			String json = "{result:true}";
			tmMemberService.updateLoginPassword(tsSysuser.getId().toString(),
					BASE64Util.getBASE64(tsSysuser.getPassword()));
			json = "{result:false}";
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(json);
		} catch (Exception e) {
			log.error("会员密码修改失败！", e);
		}
		return null;
	}

	/**
	 * 用户注册协议
	 * @return
	 */
	public String readAboutus() {
		try{
			tsAboutus = this.tsAboutusService.read(4);
		}catch (Exception e) {
			log.error("显示用户注册协议出错", e);
		}
		try {
			this.response.setCharacterEncoding("UTF-8");
			response.getWriter().print(tsAboutus.getContent());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public TmMember getTmMember() {
		return tmMember;
	}

	public void setTmMember(TmMember tmMember) {
		this.tmMember = tmMember;
	}

	public TsSysuser getTsSysuser() {
		return tsSysuser;
	}

	public void setTsSysuser(TsSysuser tsSysuser) {
		this.tsSysuser = tsSysuser;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	public String getTempId() {
		return tempId;
	}

	public void setTempId(String tempId) {
		this.tempId = tempId;
	}

	public List getProList() {
		return proList;
	}

	public void setProList(List proList) {
		this.proList = proList;
	}

	public int getReLogin() {
		return reLogin;
	}

	public void setReLogin(int reLogin) {
		this.reLogin = reLogin;
	}

	public List getCityList() {
		return cityList;
	}

	public void setCityList(List cityList) {
		this.cityList = cityList;
	}

	public static void main(String[] args) {
		System.out.println(new TmMemberAction().randLoginName());
	}
	public TsAboutus getTsAboutus() {
		return tsAboutus;
	}

	public void setTsAboutus(TsAboutus tsAboutus) {
		this.tsAboutus = tsAboutus;
	}

	public String getMailChangeFlag() {
		return mailChangeFlag;
	}

	public void setMailChangeFlag(String mailChangeFlag) {
		this.mailChangeFlag = mailChangeFlag;
	}
}
